#region Using Statements

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

#endregion

namespace Dotss.Core
{

	/// <summary>
	/// An abstraction layer for the IOC container
	/// </summary>
	public sealed class Container : IDisposable
	{

		#region Fields

		private static Container _Instance;
		private static readonly object sync = new object();

		private IUnityContainer _Unity;

		#endregion

		#region Constructors

		private Container()
		{
			InitializeContainer();
		}

		#endregion

		#region Properties

		public static Container Instance
		{
			get
			{

				if (_Instance != null) return _Instance;

				lock (sync)
				{

					if (_Instance == null)
					{

						_Instance = new Container();

					}

				}

				return _Instance;

			}
		}

		private IUnityContainer Unity
		{
			get { return _Unity; }
		}

		#endregion

		#region Private Methods

		private void InitializeContainer()
		{
			
			_Unity = new UnityContainer();
			UnityConfigurationSection config = ConfigurationManager.GetSection("unity") as UnityConfigurationSection;

			if (config != null)
				config.Containers.Default.Configure(_Unity);

		}

		#endregion

		#region Public Methods

		/// <summary>
		/// Get an object from the IoC container
		/// </summary>
		/// <typeparam name="T">Type of object to fetch</typeparam>
		/// <returns></returns>
		public T GetObject<T>()
		{
			return Unity.Resolve<T>();
		}

		public object GetObject(Type typeSought)
		{

			return Unity.Resolve(typeSought);

		}

		#endregion

		#region Implementation of IDisposable

		public void Dispose()
		{
			Unity.Dispose();
		}

		#endregion
	}

}
